package com.zou.service;

import com.zou.commons.constants.Constants;
import com.zou.commons.utils.UUIDUtils;
import com.zou.mapper.ArticleMapper;
import com.zou.mapper.ColumnMapper;
import com.zou.pojo.Column;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("columnService")
public class ColumnService {
    @Autowired(required = false)
    private ColumnMapper columnMapper;
    @Autowired(required = false)
    private ArticleMapper articleMapper;

    /**
     * 查询满足条件的栏目列表，隐藏部分数据
     *
     * @param column
     * @return
     */
    public List<Column> querySomeColumnByColumn(Column column) {
        List<Column> columns = columnMapper.selectSomeColumnByColumn(column);
        for (Column c : columns) {
            if (c.getColumnState().equals(Constants.STATE_ENABLED)) {
                c.setColumnState("启用");
            } else if (c.getColumnState().equals(Constants.STATE_DISABLED)) {
                c.setColumnState("禁用");
            }
        }
        return columns;
    }

    /**
     * 保存创建的栏目，默认序号最大
     *
     * @param column
     * @return
     */
    public boolean saveColumn(Column column) {
        column.setColumnId(UUIDUtils.getUUID());
        try {
            column.setColumnNumber(String.format("%05d", Integer.valueOf(columnMapper.maxColumnNumber()) + 1));
        } catch (NumberFormatException e) {
            e.printStackTrace();
            column.setColumnNumber("00001");
        }
        try {
            //调用mapper层方法，保存创建的栏目
            if (columnMapper.insertColumn(column) > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据Id数组批量删除栏目
     *
     * @param ids
     * @return
     */
    public boolean deleteColumnByIds(String[] ids) {
        try {
            //调用mapper层方法，删除栏目和文章
            if (articleMapper.deleteArticleByColumnIds(ids) > 0 &&
                    columnMapper.deleteColumnByIds(ids) > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据ID查询栏目
     *
     * @param id
     * @return
     */
    public Column queryColumnById(String id) {
        Column column = new Column();
        column.setColumnId(id);
        column = columnMapper.selectSomeColumnByColumn(column).get(0);
        return column;
    }

    /**
     * 修改栏目
     *
     * @param column
     * @return
     */
    public boolean editColumn(Column column) {
        try {
            //调用mapper层方法，修改栏目
            if (columnMapper.updateColumn(column) > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 置顶栏目
     *
     * @param id
     * @return
     */
    public boolean stickyColumn(String id) {
        Column column = new Column();
        column.setColumnId(id);
        column.setColumnNumber(String.format("%05d", Integer.valueOf(columnMapper.maxColumnNumber()) + 1));
        try {
            //调用mapper层方法，修改栏目
            if (columnMapper.stickyColumn(column) > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据条件分页查询栏目
     *
     * @param map
     * @return
     */
    public List<Column> queryColumnByConditionForPage(Map<String, Object> map) {
        return columnMapper.selectColumnByConditionForPage(map);
    }

    /**
     * 根据条件查询页数
     *
     * @param map
     * @return
     */
    public int queryCountOfColumnByCondition(Map<String, Object> map) {
        return columnMapper.selectCountOfColumnByCondition(map);
    }

    /**
     * 查询所有启用的栏目
     *
     * @return
     */
    public List<Column> queryColumnByStateEnabled() {
        Column column = new Column();
        column.setColumnState(Constants.STATE_ENABLED);
        List<Column> columns = columnMapper.selectSomeColumnByColumn(column);
        return columns;
    }

}
